<template>
  <!-- 所有订单 -->
  <div class="app-container">
    <!-- 筛选 -->
    <div class="filter-container">
      <el-form :inline="true" :model="listQuery">
        <!-- 关键字 -->
        <el-form-item>
          <el-input v-model="listQuery.orderNum" placeholder="请输入行程单号/客户代码" clearable @keyup.enter.native="handleFilter" style="width: 350px"> <el-button type="primary" slot="append" icon="el-icon-search" @click="handleFilter"></el-button></el-input>
        </el-form-item>
      </el-form>
    </div>

    <!-- 表格 -->
    <el-table v-loading="listLoading" :data="tableData" border fit highlight-current-row size="mini">
      <el-table-column label="#" type="index" width="50" />
      <el-table-column label="行程单号">
        <template slot-scope="{ row }">
          <div>
            <el-link type="primary" :underline="false" @click="$router.push(`/bill/detail/${row.id}`)">{{ row.orderNum }}</el-link>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="所属公司" prop="unitPrice" />
      <el-table-column label="客户代码" prop="unitPrice" />
      <el-table-column label="行程信息" width="220">
        <template slot-scope="{ row }">
          <div class="goodinfo">
            <!-- <img src="/demo.png" alt="" style="width: 220px" /> -->
            <div class="desc">
              <p>开始日期：{{ row.worksName }}；结束日期：{{ row.worksName }}</p>
              <!-- 定价销售才有艺术家 -->
              <p>航程概要：{{ rightOptionsKV[row.tubeType] }}</p>
            </div>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="飞行小时" prop="unitPrice" />
      <el-table-column label="任务空时" prop="buyNum" />
      <el-table-column label="航段数" prop="orderAmount" />
      <el-table-column label="结算费用(元)" prop="payChannel" />
    </el-table>
    <!-- 分页 -->
    <div class="m-center">
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="fetchList" />
    </div>
  </div>
</template>

<script>
import { fetchList, querySumOrderAmount } from '@/api/order'
import { buyTypeOptions, buyTypeKV, tradeStatusOptions, tradeStatusKV, tradeStatusTag, rightOptionsKV } from '@/utils/options'
import { getFile } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'Bill',
  components: { Pagination },
  data() {
    return {
      listLoading: false, // 加载动画
      tableData: [],
      total: 0,
      listQuery: {
        buyType: '', // 销售类型(0：定价销售, 1：盲盒销售)"
        endTime: '', // 下单查询结束时间
        id: 0,
        limit: 5,
        orderNum: '', // 订单编号唯一
        page: 1,
        startTime: '', // 下单查询开始时间
        tradeStatus: '', // 交易状态，0:待支付，1:支付处理中，2:取消，3:完成
        datetimerange: []
      },
      tradeStatusOptions,
      tradeStatusKV,
      tradeStatusTag,
      buyTypeOptions,
      buyTypeKV,
      rightOptionsKV,

      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      },
      sumOrderAmount: 0
    }
  },

  created() {
    // this.fetchList();
  },
  methods: {
    /*  增删改查--start */
    // 得到列表
    async fetchList() {
      this.listLoading = true
      this.getTime()
      fetchList(this.listQuery).then((response) => {
        this.tableData = response.data.items
        this.total = response.total
        this.listLoading = false

        this.querySumOrderAmount()
      })
    },

    getTime() {
      const { datetimerange } = this.listQuery
      if (datetimerange && datetimerange.length === 2) {
        this.listQuery.startTime = datetimerange[0]
        this.listQuery.endTime = datetimerange[1]
      } else {
        this.listQuery.startTime = ''
        this.listQuery.endTime = ''
      }
    },

    // 搜索
    handleFilter() {
      this.listQuery.page = 1
      this.fetchList()
    },
    querySumOrderAmount() {
      querySumOrderAmount(this.listQuery).then((response) => {
        this.sumOrderAmount = response
      })
    },

    /*  增删改查--end */
    getFile
  }
}
</script>
<style lang="scss" scoped>
.goodinfo {
  display: flex;
  img {
    width: 80px;
    margin-right: 8px;
  }
  .desc {
    text-align: left;
  }
  p {
    margin: 5px 0;
  }
}
</style>
